package com.code.generator.cn.util.base;



import com.code.generator.cn.entity.CoderField;
import com.code.generator.cn.entity.CoderTable;
import com.code.generator.cn.util.MyFileUtil;
import com.code.generator.cn.util.PathUtil;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ControllerUtil {
    public static void createController(CoderTable coderTable, List<CoderField> coderFields, Configuration configuration, String projPath) throws IOException, TemplateException {
        Writer file =null;
            String className=coderTable.getModifyName();
            Template temp=configuration.getTemplate("/code/controller.ftl");
            //获取包名
            String packageName=coderTable.getPackageName();
            String groupPath=packageName.replace(".","/");
            //生成entity的路径
         //   String mapperPath=projPath+"/src/main/java/"+groupPath+"/controller/";

            String controllerPath=PathUtil.createFileBasePath(coderTable,projPath)+"/controller/";
            //设置生成的路径
            MyFileUtil.createFileDir(controllerPath);
            //设置生成的属性
            Map<String,Object> createMap=new HashMap<>();
            CommonUtil.createBaseInfo(coderTable, createMap);
            CommonUtil.createPrimary(coderFields, createMap);
            //设置生成的文件名
            file = new FileWriter(controllerPath+className+"Controller.java");
            temp.process(createMap, file);
            file.close();

    }



}
